/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi;

import java.io.File;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;

import com.edi.common.ServerConfigure;
import com.edi.controller.InitController;
import com.edi.db.util.EdiSessionFactory;
import com.edi.manager.PluginManager;
import com.edi.manager.ServiceManager;



public class Kola {
	private static final Log log = LogFactory.getLog(InitController.class);

	private static final String CONFIG_DIR = "WEB-INF/";
	public static final String SCRIPTPLUGIN_DIR = "WEB-INF/workarea";
	
	private static ServerConfigure serverConfig;

	protected ServerConfigure configure;

	protected static EdiSessionFactory dbSessionFactory;

	protected static ServiceManager serviceManager;

	protected static PluginManager pluginManager;

	public static void init() throws Exception {
		String serverPath = System.getProperty(Constant.EDIPATH);
		File edixml = new File(serverPath + CONFIG_DIR + "kola.xml");
		if (!edixml.exists()) {
			throw new Exception("Kola init wrong,Kola.xml is not found.");
		}
		try {
			SAXReader reader = new SAXReader();

			Document document = reader.read(edixml);

			serverConfig = new ServerConfigure(document);

		} catch (Exception e) {
			log.error("Kola init wrong,Kola.xml load bad.");
			e.printStackTrace();
			log.error(e.getMessage());
		}

		dbSessionFactory = EdiSessionFactory.getInstance();
		dbSessionFactory.addSessionFactory(Constant.SERVER_APPNAME,
				serverConfig);

		serviceManager = new ServiceManager();
		pluginManager = new PluginManager();
	}
	
	public static ServiceManager getServiceManager(){
		return serviceManager;
	}
	
	public static PluginManager getPluginManager(){
		return pluginManager;
	}
	
	public static ServerConfigure getServerConfigure(){
		return serverConfig;
	}
}
